Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
React Native DateTimePicker: Why is it keep opening after I press the "OK" buton

I'm trying to create a date picker. When the user press on the "OK" button the modal should have closed but it keeps reopening. I'm using react-native-datetimepicker. I did tried putting setShow(false) when the "OK" button is pressed but it still open for a split second.

const [receiptDate, setReceiptDate] = React.useState("");
const [date, setDate] = React.useState(new Date()); 
const [today, setToday] = React.useState(new Date()); 
const [show, setShow] = React.useState(false); 

const onChange = (event, selectedDate) => {
    const currentDate = selectedDate || date;
    setDate(currentDate);
    if (event.type == "dismissed") {  
        console.log("CANCEL")
        setShow(false) 
        return null;
    } else { 
        setShow(false)
        console.log("OK")
        let tempDate = new Date(currentDate);
        let fDate = tempDate.getFullYear() + "-" + tempDate.getDate() + "-" + (tempDate.getMonth() + 1)
        setReceiptDate(fDate);
        console.log(fDate);
        return;
    }
}

                     <View>
                        <TouchableOpacity activeOpacity={0.9} onPress={() => setShow(true)}>
                            <TextInput
                                editable={false} 
                                placeholder="YYYY-MM-DD (2022-05-22)" 
                                value={receiptDate}
                            />
                        </TouchableOpacity>
                        {show && (
                            <DateTimePicker
                                testID='dateTimePicker'
                                value={date}
                                mode={'date'} 
                                display='default'
                                onChange={onChange}
                                maximumDate={today}
                        />)}
                    </View>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In my opinion, you are not setting show to false, in case if the user selects an option. Please try this and let me know if it fixes your problem or not:

const onChange = (event, selectedDate) => {
        if (event.type == 'set') {
            const currentDate = selectedDate || startDate;
            let tempDate = new Date(currentDate);
            let fDate = tempDate.getFullYear() + "-" + tempDate.getDate() + "-" + (tempDate.getMonth() + 1)
            setReceiptDate(fDate);
            setDate(currentDate);
        }
        setShow(false) 
    }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda